package cn.base.web.support;

import cn.rengy.lang.DataConverter;
import cn.rengy.lang.DataConverterException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.Map;

@Component
@Slf4j
public class DataConverterImpl implements DataConverter {

	@Autowired
	private ObjectMapper objectMapper;
	
	private XmlMapper xmlMapper=new XmlMapper();
	
	
	@Override
	public String beanToXml(Object bean) {
		try {
			return xmlMapper.writeValueAsString(bean);
		} catch (JsonProcessingException e) {
			log.error("beanToXml：{}",bean.toString());
			throw new DataConverterException(e);
		}
	}


	@Override
	public String beanToJson(Object bean) {
		try {
			return objectMapper.writeValueAsString(bean);
		} catch (JsonProcessingException e) {
			log.error("beanToJson：{}",bean.toString());
			throw new DataConverterException(e);
		}
	}

	@Override
	public String mapToJson(Map map) {
		try {
			return objectMapper.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			log.error("mapToJson错误：{}",map.toString());
			throw new DataConverterException(e);
		}
	}

	@Override
	public String toJson(Object object) {
		try {
			return objectMapper.writeValueAsString(object);
		} catch (JsonProcessingException e) {
			log.error("toJson错误：{}",object.toString());
			throw new DataConverterException(e);
		}
	}

	@Override
	public <T> T jsonToBean(String jsonStr, Class<T> valueType)  {
		try {
			return objectMapper.readValue(jsonStr, valueType);
		} catch (JsonProcessingException e) {
			log.error("jsonToBean错误：{}",jsonStr);
			throw new DataConverterException(e);
		}
	}

	@Override
	public Map jsonToMap(String jsonStr) {
		try {
			return objectMapper.readValue(jsonStr,Map.class);
		} catch (JsonProcessingException e) {
			log.error("jsonToMap错误：{}",jsonStr);
			throw new DataConverterException(e);
		}
	}


	@Override
	public <T> T xmlToBean(String xml, Class<T> valueType)  {
		try {
			return xmlMapper.readValue(xml, valueType);
		} catch (JsonProcessingException e) {
			log.error("xmlToBean错误：{}",xml);
			throw new DataConverterException(e);
		}
	}
	
	@Override
	public <T> T xmlToBean(InputStream inputStream, Class<T> valueType) {
		try {
			return xmlMapper.readValue(inputStream, valueType);
		} catch (Exception e) {
			log.error("xmlinputStreamToBean错误");
			throw new DataConverterException(e);
		}
	}

	@Override
	public Map<String, Object> xmlToMap(String xml) {

		try {
			return xmlMapper.readValue(xml, Map.class);
		} catch (Exception e) {
			log.error("xmlToMap错误：{}",xml);
			throw new DataConverterException(e);
		}
	}

	@Override
	public Object jsonToObject(String json)  {
		Object obj=null;
		if(StringUtils.isNotBlank(json)){
			char c=json.charAt(0);
			if(c=='{') {
				try {
					obj=objectMapper.readValue(json, Map.class);
				} catch (JsonProcessingException e) {
					log.error("readValue错误：{}",json);
					throw new DataConverterException(e);
				}
			}else if(c=='['){
				try {
					obj=objectMapper.readValue(json, Object[].class);
				} catch (JsonProcessingException e) {
					log.error("readValue错误：{}",json);
					throw new DataConverterException(e);
				}
			}else{
				obj=json;
			}
		}
		return obj;
	}

}
